package com.mjh.task;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.mjh.manager.*;
import com.mjh.comm.*;

public class LuceneIndexMgr implements Job{
	
	private static Logger log = Logger.getLogger(LuceneIndexMgr.class);
	
	private ArticleSearchManager manager = new ArticleSearchManager();
	
	public void execute(JobExecutionContext context) throws JobExecutionException {
		
		String jobName = context.getJobDetail().getName();
		TaskManager.getInstance().setTaskLastExcuteDate(jobName, context.getFireTime());
		TaskManager.getInstance().setTaskNextExcuteDate(jobName, context.getNextFireTime());
	
		int addResult = Constant.FAIL;
		int createResult = Constant.FAIL;
		int updateResult = Constant.FAIL;
		
		if(manager.isIndexExist()){
			addResult = manager.addIndex();
			log.info("添加索引  " + Constant.OPERATE_DESC[addResult]);
		} else {
			createResult = manager.createIndex();
			log.info("创建索引  " + Constant.OPERATE_DESC[createResult]);
		}
		updateResult = manager.updateIndex();
		log.info("修改索引  " + Constant.OPERATE_DESC[updateResult]);
		
		//上面任何一个操作失败都认为任务失败
		int result = addResult * createResult * updateResult;
		TaskManager.getInstance().setTaskLastExcuteState(jobName, result);
	}

}
